package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信相关接口对接实现类
 *
 * @Author mr.wu
 * @Date 2025-10-22 15:01
 */
@Service
public class WechatServiceImpl implements WechatService {

    @Value("${wechat.appId}")
    private String appId;

    @Value("${wechat.secret}")
    private String secret;

    private static final String OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session"; //小程序登录接口地址
    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";//获取访问凭证接口地址
    private static final String PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";//获取手机号信息接口地址


    /**
     * 获取微信唯一身份标识openid
     *
     * @param code
     * @return
     */
    @Override
    public String getOpenId(String code) {
        Map<String,Object> paramMap = this.getCommonParamMap();
        paramMap.put("js_code", code); //临时授权码
        paramMap.put("grant_type", "authorization_code");//授权类型，固定字符串

        String responseBody = HttpUtil.get(OPENID_URL, paramMap);
        if(StrUtil.isBlank(responseBody)) {
            throw new BaseException("获取openId失败");
        }
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        String openId = jsonObject.getStr("openid");
        return openId;
    }

    /**
     * 构建公共参数
     * @return
     */
    private Map<String,Object> getCommonParamMap() {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("appid", appId); //小程序id
        paramMap.put("secret", secret); //小程序密钥
        return paramMap;
    }


    /**
     * 获取接口访问凭证
     * @return
     */
    private String getAccessToken() {
        Map<String,Object> paramMap = this.getCommonParamMap();
        paramMap.put("grant_type","client_credential");//授权类型，固定字符串
        String responseBody = HttpUtil.get(ACCESS_TOKEN_URL, paramMap);
        if(StrUtil.isBlank(responseBody)) {
            throw new BaseException("获取accessToken失败");
        }
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        String accessToken = jsonObject.getStr("access_token");
        return accessToken;
    }

    /**
     * 获取微信用户的手机号
     *
     * @param code
     * @return
     */
    @Override
    public String getPhoneNum(String code) {
        String requestBody = JSONUtil.toJsonStr(Map.of("code", code));
        String responseBody = HttpUtil.post(PHONE_URL + this.getAccessToken(), requestBody);
        if(StrUtil.isBlank(responseBody)) {
            throw new BaseException("获取phoneNum失败");
        }
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        String phoneInfoJSON = jsonObject.getStr("phone_info");
        if(StrUtil.isBlank(phoneInfoJSON)) {
            throw new BaseException("获取phoneNum失败");
        }

        JSONObject phoneInfoJsonObject = JSONUtil.parseObj(phoneInfoJSON);
        return phoneInfoJsonObject.getStr("phoneNumber");
    }
}
